/**
 * 设置界面
 */
/**
 * 设置类型
 */
var Setting_TYPE = {
    Hall: 0,
    Table_Mj: 1,
    Table_Pdk: 2,
    Table_Psz: 3,
    Table_Hz: 4,
    Table_Nn: 5,
    Table_Fsp: 6,
    Table_Epz: 7,
    Table_Kaokao: 8,
    Table_dou14: 9,
};
var SettingWindow = Window.extend({
    _ccsFileName: res.SettingWindow_json,
    _type: null,
    initWindow: function (type) {
        this._type = type || Setting_TYPE.Hall;
        this._initBtns();
    },
    /**
     * 初始化按钮
     * @protected
     */
    _initBtns: function () {
        var self = this;
        this._changeBtn(self._nodeList['root.btn_music'], settingData.musicvoice);
        this._changeBtn(self._nodeList['root.btn_yinxiao'], settingData.yinxiaovoice);
        this._changeBtn(self._nodeList['root.btn_biaoqing'], settingData.pingBiBiaoQing);
        this._initBtnState();
        this._initBgmChoose();
        this._initBgChoose();
        this._initPaiChoose();
        this._initYuYanChoose();
        this._initBgTexture();
    },
    _initClickListeners: function () {
        this._super();
        this._clickListeners = {
            'root.btn_yinxiao': '_clickYinXiao',
            'root.btn_music': '_clickMusic',
            'root.btn_tuichu': '_clickExit',
            'root.btn_biaoqing': '_clickBiaoQing',
            'root.btn_refresh': '_clickRefresh',
        };
    },
    /**
     * 修复版本
     * @private
     */
    _clickRefresh: function () {
        showXiuFuWindow();
    },
    /**
     * 初始化按钮状态
     * @private
     */
    _initBtnState: function () {
        this._nodeList['root.btn_tuichu'].setVisible(this._type == Setting_TYPE.Hall);
    },
    /**
     * 初始化语言选项
     * @private
     */
    _initYuYanChoose: function () {
        var self = this;
        var soundNode = this._nodeList['root.soundNode'];
        if (!soundNode) {
            return;
        }
        var isFangyan = (this._type == Setting_TYPE.Table_Mj || this._type == Setting_TYPE.Table_Fsp || this._type == Setting_TYPE.Table_Hz);
        var isDialect = settingData.local_dialect[gameData.mapId];

        soundNode.setVisible(isFangyan);
        soundNode["fangyan"].setVisible(isFangyan);
        soundNode["txt_fangyan"].setVisible(isFangyan);
        soundNode['fangyan'].addEventListener(function () {
            self._changeDialect(true);
        });

        //关闭普通话选择
        if (!isFangyan && false) {
            soundNode["putonghua"].setPosition(soundNode["fangyan"].getPosition());
            soundNode["txt_putonghua"].setPosition(soundNode["txt_fangyan"].getPosition());
        }
        soundNode["putonghua"].setVisible(false);
        soundNode["txt_putonghua"].setVisible(false);
        return;
        if (this._type == Setting_TYPE.Table_Fsp) {
            soundNode["putonghua"].setVisible(false);
            soundNode["txt_putonghua"].setVisible(false);
        }
        soundNode['putonghua'].addEventListener(function () {
            self._changeDialect(false);
        });
        TouchUtil.setOnclickListener(soundNode['txt_fangyan'], function () {
            self._changeDialect(true);
        });
        TouchUtil.setOnclickListener(soundNode['txt_putonghua'], function () {
            self._changeDialect(false);
        });
        self._changeDialect(isDialect && soundNode['fangyan'].isVisible());
    },
    /**
     * 切换语言
     * @param isDialect
     * @private
     */
    _changeDialect: function (isDialect) {
        if (gameData.mapId) {
            var local_dialect = settingData.local_dialect;
            local_dialect[gameData.mapId] = isDialect;
            settingData.local_dialect = local_dialect;
            var soundNode = this._nodeList['root.soundNode'];
            soundNode['fangyan'].setSelected(isDialect);
            soundNode['putonghua'].setSelected(!isDialect);
        }
    },
    /**
     * 初始化背景音乐选项
     * @private
     */
    _initBgmChoose: function () {
        var self = this;
        var isBgmChoose = (this._type == Setting_TYPE.Table_Mj || this._type == Setting_TYPE.Table_Fsp || this._type == Setting_TYPE.Table_Hz);
        self._nodeList['root.musicNode'].setVisible(isBgmChoose);
        if (isBgmChoose) {
            self._clickChangeBgm(settingData.local_bgMusic[gameData.mapId] || 1);

            for (var i = 1; i <= 3; i++) {
                (function (i) {
                    self._nodeList['root.musicNode.music_' + i].addEventListener(function () {
                        self._clickChangeBgm(i);
                    });
                    TouchUtil.setOnclickListener(self._nodeList['root.musicNode.szmusic' + i], function () {
                        self._clickChangeBgm(i);
                    });
                })(i);
            }
        }
    },
    /**
     * 改变bgm
     * @private
     */
    _clickChangeBgm: function (id) {
        var self = this;
        for (var i = 1; i <= 3; i++) {
            self._nodeList['root.musicNode.music_' + i].setSelected(i == id);
        }
        if (this._type == Setting_TYPE.Hall) {
            settingData.bgmId = id;
        } else {
            var local_bgMusic = settingData.local_bgMusic;
            local_bgMusic[gameData.mapId] = id;
            settingData.local_bgMusic = local_bgMusic;
        }
        eventManager.dispatchEvent(EventKey.ChangeBgm, id);
    },
    /**
     * 点击静音
     * @param {cc.Node} sender
     * @protected
     */
    _clickJingYin: function (sender) {
        settingData.jingyinopen = !settingData.jingyinopen;
        this._changeBtn(sender, settingData.jingyinopen);
        if (settingData.yinxiaovoice && settingData.musicvoice) {
            resumeMusic();
        } else {
            pauseMusic();
        }
    },
    /**
     * 点击理牌
     * @param {cc.Node} sender
     * @protected
     */
    _clickLiPai: function (sender) {
        settingData.sortFlag = -1 * settingData.sortFlag;
        this._changeBtn(sender, settingData.sortFlag == 1);
    },
    /**
     * 点击音效
     * @param {cc.Node} sender
     * @protected
     */
    _clickYinXiao: function (sender) {
        settingData.yinxiaovoice = !settingData.yinxiaovoice;
        this._changeBtn(sender, settingData.yinxiaovoice);
    },
    /**
     * 点击音乐
     * @param {cc.Node} sender
     * @protected
     */
    _clickMusic: function (sender) {
        settingData.musicvoice = !settingData.musicvoice;
        this._changeBtn(sender, settingData.musicvoice);
        if (settingData.musicvoice) {
            resumeMusic();
        } else {
            pauseMusic();
        }
    },
    /**
     * 点击表情
     * @param {cc.Node} sender
     * @protected
     */
    _clickBiaoQing: function (sender) {
        settingData.pingBiBiaoQing = !settingData.pingBiBiaoQing;
        this._changeBtn(sender, settingData.pingBiBiaoQing);
    },
    /**
     * 点击解散
     * @protected
     */
    _clickJieSan: function () {
        eventManager.dispatchEvent(EventKey.ReceiveShenQingJieSan);
        this.close();
    },

    /**
     * 点击退出
     * @protected
     */
    _clickExit: function () {
        showMessageBox('是否确定退出登录？', function () {
            cc.sys.localStorage.removeItem('wx_openid');
            cc.sys.localStorage.removeItem('xl_openid');
            cc.sys.localStorage.removeItem('lb_openid');
            gameData.hasLogined = false;
            if (window.hasOwnProperty('isYaYaYule')) {
                cc.sys.localStorage.removeItem('current_app');
                network.disconnect(true);
            } else {
                network.disconnect();
            }
        });
    },
    /**
     * 改变按钮
     * @param {cc.Node} btn
     * @param {Boolean} onOff
     * @protected
     */
    _changeBtn: function (btn, onOff) {
        btn.getChildByName('on').setVisible(onOff);
        btn.getChildByName('off').setVisible(!onOff);
    },
    /**
     * 设置背景贴图
     * @private
     */
    _clickChangebg: function (id) {
        this._changebgTab(id);
        var tableid = settingData.HALL_BG_Id;
        settingData.HALL_BG_Id = id;
        if (id != tableid) {
            eventManager.dispatchEvent(EventKey.HallBoardRoomBGEvent, id);
        }
    },
    _initBgTexture: function () {
        var self = this;
        var paizhuoNode = this._nodeList['root.hallbgnode'];
        if (!paizhuoNode) {
            return;
        }
        var isPaiZhuoChoose = (this._type == Setting_TYPE.Hall );
        paizhuoNode.setVisible(isPaiZhuoChoose);
        if (!isPaiZhuoChoose) {
            return;
        }
        var tableid = settingData.HALL_BG_Id;
        self._clickChangebg(tableid);
        for (var i = 0; i < 3; i++) {
            (function (i) {
                TouchUtil.setOnclickListener(self._nodeList['root.hallbgnode.btn_table_' + i], function () {
                    self._clickChangebg(i);
                });
            })(i);
        }
    },
    /**
     * 初始化背景选择
     * @private
     */
    _initBgChoose: function () {
        var self = this;
        var paizhuoNode = this._nodeList['root.paizhuoNode'];
        if (!paizhuoNode) {
            return;
        }
        var isPaiZhuoChoose = (this._type == Setting_TYPE.Table_Hz);
        paizhuoNode.setVisible(isPaiZhuoChoose);
        if (!isPaiZhuoChoose) {
            return;
        }
        var tableid = settingData.local_table[gameData.mapId];
        self._clickChangeTable(tableid);
        self._initBgTexture();
        self._nodeList['root.paizhuoNode.majiang'].setVisible(this._type == Setting_TYPE.Table_Mj);
        self._nodeList['root.paizhuoNode.zipai'].setVisible(this._type == Setting_TYPE.Table_Hz);
        if (this._type == Setting_TYPE.Table_Mj) {
            for (var i = 0; i < 3; i++) {
                (function (i) {
                    TouchUtil.setOnclickListener(self._nodeList['root.paizhuoNode.majiang.btn_table_' + i], function () {
                        self._clickChangeTable(i);
                    });
                })(i);
            }
        } else {
            for (var i = 0; i < 3; i++) {
                (function (i) {
                    TouchUtil.setOnclickListener(self._nodeList['root.paizhuoNode.zipai.btn_table_' + i], function () {
                        self._clickChangeTable(i);
                    });
                })(i);
            }
        }
    },
    /**
     * 点击改变桌面
     * @private
     */
    _clickChangeTable: function (id) {
        this._changeTableTab(id);
        var tableid = settingData.local_table[gameData.mapId];
        var local_table = settingData.local_table;
        local_table[gameData.mapId] = id;
        settingData.local_table = local_table;
        if (id != tableid) {
            eventManager.dispatchEvent(EventKey.ChangeTable, id);
        }
    },
    /**
     * 改变桌面id（纯显示）
     * @param id
     * @private
     */
    _changeTableTab: function (id) {
        for (var i = 0; i < 3; i++) {
            var light = this._nodeList['root.paizhuoNode.zipai.btn_table_' + i + '.select_bg'];
            if (light) {
                light.setVisible(i == id);
            }
        }
    },
    /**
     * 改变桌面id（纯显示）
     * @param id
     * @private
     */
    _changebgTab: function (id) {
        for (var i = 0; i < 3; i++) {
            var light = this._nodeList['root.hallbgnode.select_bg'+i];
            if (light) {
                light.setVisible(i == id);
            }
        }
    },
    /**
     * 初始化牌面选择
     * @private
     */
    _initPaiChoose: function () {
        var self = this;
        var paimianNode = this._nodeList['root.paimianNode'];
        if (!paimianNode) {
            return;
        }
        var isPaiChoose = (this._type == Setting_TYPE.Table_Hz);
        //暂时屏蔽排面选择
        //paimianNode.setVisible(isPaiChoose);
        paimianNode.setVisible(false);
        return;
        if (!isPaiChoose) {
            return;
        }
        self._nodeList['root.paimianNode.zipai'].setVisible(this._type == Setting_TYPE.Table_Hz);
        self._nodeList['root.paimianNode.majiang'].setVisible(this._type == Setting_TYPE.Table_Mj);
        if(this._type == Setting_TYPE.Table_Hz){
            var tableid = settingData.hz_pai || 0;
            self._clickChangePai(tableid);
            for (var i = 0; i < 3; i++) {
                (function (i) {
                    TouchUtil.setOnclickListener(self._nodeList['root.paimianNode.zipai.btn_table_' + i], function () {
                        self._clickChangePai(i);
                    });
                    TouchUtil.setOnclickListener(self._nodeList['root.paimianNode.zipai.btn_table_' + i + '.Image_bg'], function () {
                        self._clickChangePai(i);
                    });
                })(i);
            }
        }else if(this._type == Setting_TYPE.Table_Mj){
            var tableid = settingData.mj_pai || 0;
            self._clickChangePai(tableid);
            for (var i = 0; i < 3; i++) {
                (function (i) {
                    TouchUtil.setOnclickListener(self._nodeList['root.paimianNode.majiang.btn_table_' + i], function () {
                        self._clickChangePai(i);
                    });
                    TouchUtil.setOnclickListener(self._nodeList['root.paimianNode.majiang.btn_table_' + i + '.Image_bg'], function () {
                        self._clickChangePai(i);
                    });
                })(i);
            }
        }
    },
    /**
     * 点击改变牌面
     * @private
     */
    _clickChangePai: function (id) {
        this._changePaiTab(id);
        var tableid = settingData.mj_pai;
        if(this._type == Setting_TYPE.Table_Hz){
            tableid = settingData.hz_pai;
            settingData.hz_pai = id;
        }else if(this._type == Setting_TYPE.Table_Mj){
            tableid = settingData.mj_pai;
            settingData.mj_pai = id;
        }
        if (id != tableid) {
            eventManager.dispatchEvent(EventKey.ChangePai, id);
        }
    },
    /**
     * 改变牌面id（纯显示）
     * @param id
     * @private
     */
    _changePaiTab: function (id) {
        if(this._type == Setting_TYPE.Table_Hz){
            for (var i = 0; i < 3; i++) {
                var light = this._nodeList['root.paimianNode.zipai.btn_table_' + i + '.select_bg'];
                if (light) {
                    light.setVisible(i == id);
                }
            }
        }else if(this._type == Setting_TYPE.Table_Mj) {
            for (var i = 0; i < 3; i++) {
                var light = this._nodeList['root.paimianNode.majiang.btn_table_' + i + '.select_bg'];
                if (light) {
                    light.setVisible(i == id);
                }
            }
        }
    },
});